//
//  BaseNavigationController.m
//  QSF_Dev
//
//  Created by Mr.Dynasty on 2018/5/24.
//  Copyright © 2018年 Mr.Dynasty. All rights reserved.
//

#import "BaseNavigationController.h"

@interface BaseNavigationController ()

@end

@implementation BaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

+ (void)initialize {
    
    [UINavigationBar appearance].translucent = NO;
    
    //导航栏去除下划线
    [[UINavigationBar appearance] setBackgroundImage:[UIImage ssn_imageWithColor:[UIColor whiteColor]] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [UINavigationBar appearance].shadowImage = [UIImage ssn_imageWithColor:DefaultColor];
    
    [UINavigationBar appearance].tintColor = HEX_COLOR(0x393939);
    

    // iOS11  状态栏20差
    
    if (@available(iOS 11.0, *)){
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }
    
    
}

+ (void)load {
    [super load];
    UINavigationBar *navigationBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
    dict[NSForegroundColorAttributeName] = HEX_COLOR(0xF7CE47);
    [navigationBar setTitleTextAttributes:dict];
    [navigationBar setBackgroundImage:[UIImage ssn_imageWithColor:DefaultColor] forBarMetrics:UIBarMetricsDefault];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    NSInteger count = self.childViewControllers.count;
    if (count > 0) {
        
        if ([viewController isKindOfClass:[BaseViewController class]] || [viewController isKindOfClass:[BaseTableViewController class]]) {
            
            viewController.hidesBottomBarWhenPushed = YES;
            
            UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            
            if ([viewController isKindOfClass:[BaseViewController class]] || [viewController isKindOfClass:[BaseTableViewController class]]) {
                ((BaseViewController *)viewController).leftBtn = leftBtn;
            }
            
            leftBtn.frame = CGRectMake(0, 0, 44, 44);
            [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(14.5, 0, 14.5, 36)];
            [leftBtn setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
            [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
            UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
            viewController.navigationItem.leftBarButtonItem = leftItem;
            
        }
    }
    [super pushViewController:viewController animated:animated];
    [self changeTabbarFrame];
}

- (void)changeTabbarFrame {
    
    // 修改tabBra的frame
    CGRect frame = self.tabBarController.tabBar.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
    self.tabBarController.tabBar.frame = frame;
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    return [super popViewControllerAnimated:animated];
}

- (void)back {
    
    BaseViewController *baseVc = (BaseViewController *)self.topViewController;
    if ([baseVc isKindOfClass:[BaseViewController class]]||[baseVc isKindOfClass:[BaseTableViewController class]]) {
        [baseVc actionForLeft:nil];
        
    }else{
        
        NSLog(@"\n--- ViewController must inherit from the BaseViewController! ---");
        return;
    }
}
@end
